package com.sb.money.services.impl;

import com.sb.money.dao.SubcategoryDao;
import com.sb.money.entities.Subcategory;
import com.sb.money.exception.MoneyServiceException;
import com.sb.money.services.SubcategoryService;
import com.sb.money.util.MessagesUtil;

import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

/**
 * @author Stefan Bertos
 */
public class SubcategoryServiceImpl implements SubcategoryService {

    /**
     * Inject logger.
     */
    @Inject
    private Logger logger;

    /**
     * Inject dao.
     */
    @Inject
    private SubcategoryDao dao;

    /**
     * default constructor.
     */
    public SubcategoryServiceImpl() {
        super();
    }

    @Log
    @Override
    public final Subcategory create(final Subcategory entity)
            throws MoneyServiceException {
        try {
            final Subcategory result = this.dao.create(entity);
            this.logger.info(MessagesUtil.message("info.dao.create", entity));
            return result;
        } catch (Exception e) {
            throw new MoneyServiceException(MessagesUtil.message(
                    "error.dao.create", entity ),e);
        }
    }

    @Log
    @Override
    public final void update(final Subcategory entity)
            throws MoneyServiceException {
        try {
            this.dao.update(entity);
            this.logger.info(MessagesUtil.message("info.dao.update", entity));
        } catch (Exception e) {
            throw new MoneyServiceException(MessagesUtil.message(
                    "error.dao.update", entity),e);
        }
    }

    @Log
    @Override
    public final void delete(final Subcategory entity)
            throws MoneyServiceException {
        try {
            this.dao.delete(entity);
            this.logger.info(MessagesUtil.message("info.dao.delete", entity));
        } catch (Exception e) {
            throw new MoneyServiceException(MessagesUtil.message(
                    "error.dao.delete", entity),e);
        }
    }

    @Log
    @Override
    public final List<Subcategory> findAll() throws MoneyServiceException {
        try {
            final List<Subcategory> result = this.dao.findAll();
            if (result == null || result.isEmpty()) {
                this.logger.warn(MessagesUtil.message("warn.dao.findAll",
                        Subcategory.class.getCanonicalName()));
            } else {
                this.logger.info(MessagesUtil.message("info.dao.findAll",
                        Subcategory.class.getCanonicalName()));
            }
            return result;
        } catch (Exception e) {
            throw new MoneyServiceException(MessagesUtil.message(
                    "error.dao.findAll", Subcategory.class.getCanonicalName()
                   ),e);
        }
    }
}